package de.slothsoft.commons.swing.util;

import javax.swing.ComboBoxModel;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import de.slothsoft.commons.IDisplayable;
import de.slothsoft.commons.swing.JCheckList;
import de.slothsoft.commons.swing.KeyControl;
import de.slothsoft.commons.swing.databinding.DataBinding;

public final class DataBindingUtil {

	public static <T> KeyControl createKeyControl(DataBinding<T> dataBinding, JComponent component, String property,
			int x, int y) {
		KeyControl keyControl = ControlUtil.createKeyControl(component, x, y);
		dataBinding.bind(keyControl, dataBinding.getModel(), property);
		return keyControl;
	}

	public static <T> JTextField createText(DataBinding<T> dataBinding, JComponent component, String property, int x,
			int y) {
		JTextField textField = ControlUtil.createText(component, x, y);
		dataBinding.bind(textField, dataBinding.getModel(), property);
		return textField;
	}

	public static <T> JTextField createPassword(DataBinding<T> dataBinding, JComponent component, String property,
			int x, int y) {
		JTextField textField = ControlUtil.createPassword(component, x, y);
		dataBinding.bind(textField, dataBinding.getModel(), property);
		return textField;
	}

	public static <T> JTextArea createTextArea(DataBinding<T> dataBinding, JComponent component, String property,
			int x, int y) {
		JTextArea textArea = ControlUtil.createTextArea(component, x, y);
		textArea.setWrapStyleWord(true);
		textArea.setLineWrap(true);
		dataBinding.bind(textArea, dataBinding.getModel(), property);
		return textArea;
	}

	public static <T> JCheckBox createCheckBox(DataBinding<T> dataBinding, JComponent component, String property,
			int x, int y) {
		JCheckBox check = ControlUtil.createCheckBox(component, x, y);
		dataBinding.bind(check, dataBinding.getModel(), property);
		return check;
	}

	public static <T, C> JComboBox<C> createCombo(DataBinding<T> dataBinding, JComponent parent, String property,
			ComboBoxModel<C> model, int x, int y) {
		JComboBox<C> combo = ControlUtil.createCombo(parent, model, x, y);
		dataBinding.bind(combo, dataBinding.getModel(), property);
		return combo;
	}

	public static <T, D extends IDisplayable> JCheckList<D> createCheckList(DataBinding<T> dataBinding,
			JComponent parent, String property, D[] elements, int x, int y) {
		JCheckList<D> checkList = ControlUtil.createCheckList(parent, elements, x, y);
		dataBinding.bind(checkList, dataBinding.getModel(), property);
		return checkList;
	}

	private DataBindingUtil() {
		// I'm electricity! (i.e. invisible)
	}

}
